import requests
from jsonpath import jsonpath
import csv
import re


class IndexSpider(object):
    def __init__(self):
        # wid接口
        self.index_url = 'https://data.weibo.com/index/ajax/newindex/searchword'
        # 接口参数
        self.index_payload = {'word': ''}
        self.url = "https://data.weibo.com/index/ajax/newindex/getchartdata"
        self.payload={
            'wid':'1091324292793',
            'dateGroup':'1hour'
        }
        self.headers = {
          'cookie': 'SINAGLOBAL=8988227825377.598.1659433769352; UOR=www.baidu.com,weibo.com,graph.qq.com; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W50ZpV8QRGbaof--s1NXFBZ5JpX5KMhUgL.FoMRe0epS0MEShz2dJLoIp7LxKML1KBLBKnLxKqL1hnLBoMpe0MXSKn41KM0; ULV=1683641285114:7:2:2:3508939845800.7847.1683641285101:1683553658807; SCF=AhucQKsZDPwkoBsO7r9b4whb_L8uwRN75Cqw_xYgK5WG1GXkbDUeRMn4Wotwbruj_c79Uv4sq9OS1jD6YR-2eMg.',
          'referer': 'https://data.weibo.com/index/newindex?visit_type=trend&wid=1091324292793',
          'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36',
          'Content-Type': 'application/x-www-form-urlencoded'
        }
        self.f = open('weibozs.csv', 'w', newline='', encoding='utf-8')
        self.csv_w = csv.writer(self.f)
        self.csv_w.writerow(['时间', '指数'])

    def get_data(self,wid):
        self.payload['wid']=wid
        response = requests.post(self.url, headers=self.headers, data=self.payload)
        # print(response.json())
        return response.json()

    def parse_data(self, response_data):
        index_times = jsonpath(response_data, '$..x')[0]
        indexs = jsonpath(response_data, '$..s')[0]
        print(index_times)
        print(indexs)
        for index_time, index in zip(index_times, indexs):
            print(index_time, '--------->>', index)
            self.csv_w.writerow([index_time, index])

    def parse_index_data(self):
        """获取wid"""
        response = requests.post(self.index_url, headers=self.headers, data=self.index_payload)
        json_data = response.text
        # print(json_data)
        wid = re.findall(r'"html":"<li\swid=\\"(.*?)\\"\sword=', json_data, re.S)
        #print(wid)
        return wid[0]

    # def run(self):
    #     data = self.get_data()
    #     self.parse_data(data)
    #     self.f.close()


if __name__ == '__main__':

    s = IndexSpider()
    print("请输入你想要查询指数的词汇:",end='')
    word=input()
    s.index_payload['word']=word
    #s.run()
    wid=s.parse_index_data()
    data=s.get_data(wid)
    s.parse_data(data)

    """
    https://m.weibo.cn/detail/4813628149072458
    """







